#include<iostream>

using namespace std;


void permutation(char * str, char* begin ){
	// 方法：先固定一个字符，再寻找后面所有字符的排序
	if ( *begin == '\0' ){
		cout << str << endl;
	}else {
		for ( char * ch = begin; *ch != '\0'; ch ++ ){
		// 交换字符
		char temp = *ch;
		*ch = *begin;
		*begin = temp;
		permutation(str, begin+1);
		
		// 再将交换的字符交换到原来的字符
		temp = *begin;
		*begin = *ch;
		*ch = temp;
		}
	}
}

void permutation(char * str){
	if ( str == nullptr ){
		return;
	}

	permutation(str, str);
}

void Test(char* pStr){
    if(pStr == nullptr)
        cout <<  "Test for nullptr begins: " << endl;
    else
        cout<< "Test for" << pStr << "begins:" << endl;

    permutation(pStr);
    cout << "---------------" << endl;
}


int main(void){
    char string1[5] = "abcd";
	Test(string1);
    char string2[] = "a";
    Test(string2);
    char string3[] = "";
    Test(string3);
    return 0;
}